package com.ratta.supernote.note.plugincore.ui.menu;


import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;

import java.util.List;
import java.util.Map;

import kotlin.jvm.Transient;

/**
 * Plugin menu item base class
 */
public class PluginMenuItem {


    /**
     * Sidebar button type, not serialized
     */
    @Transient
    public static final int TYPE_SIDE = 1;

    /**
     * Edit menu button, not serialized
     */
    @Transient
    public static final int TYPE_EDIT = 2;

    // Plugin ID
    protected String pluginID = "";

    // Plugin name
    protected String pluginName = "";

    /**
     * MenuItem type:
     * 1: Sidebar button
     * 2: Lasso toolbar button
     */
    protected int type;


    /**
     * Parent menu ID
     */
    protected int parentId;

    /**
     * Button ID, unique and immutable, defined by plugin
     */
    protected int id;

    /**
     * Button name in JSON format, example:
     * {
     * "zh_CN": "Simplified Chinese",
     * "zh_TW": "Traditional Chinese",
     * "en": "English"
     * }
     */
    protected String name;

    /**
     * Button name map, key is current language, value is name in that language
     */
    protected Map<String, String> nameMap;


    /**
     * Font color
     */
    protected int color;

    /**
     * Image path
     */
    protected String icon;


    /**
     * Background color
     */
    protected int bgColor;

    /**
     * Whether plugin is enabled
     */
    protected boolean enable = true;

    /// Button description
    protected String desc = "";

    /**
     * 按钮描述，key是当前语言，value是语言对应的翻译
     */
    protected Map<String, String> descMap;

    /**
     * Whether button is enabled
     */
    protected boolean menuEnable = true;

    /**
     * Display information after button click, can be null
     */
    protected PluginMenuItemShowData showData;

    /**
     * Secondary submenus
     */
    protected List<PluginMenuItem> subMenuItems;

    /**
     * Last used time
     */
    protected long recentUsedTime = 0;

    /**
     * Button hierarchy level
     * 1: First level menu
     * 2: Second level menu
     */
    protected int menuLevel = 2;

    public PluginMenuItem() {
    }

    public PluginMenuItem(String pluginID, String pluginName, int type, int parentId, int id, String name, int color, String icon, int bgColor, PluginMenuItemShowData showData, List<PluginMenuItem> subMenuItems,
                          boolean menuEnable, String desc) {
        this.pluginID = pluginID;
        this.pluginName = pluginName;
        this.type = type;
        this.parentId = parentId;
        this.id = id;
        this.name = name;
        this.color = color;
        this.icon = icon;
        this.bgColor = bgColor;
        this.showData = showData;
        this.subMenuItems = subMenuItems;
        this.menuEnable = menuEnable;
        this.desc = desc;
        parseName();
    }

    public String getPluginID() {
        return pluginID;
    }

    public void setPluginID(String pluginID) {
        this.pluginID = pluginID;
    }

    public String getPluginName() {
        return pluginName;
    }

    public void setPluginName(String pluginName) {
        this.pluginName = pluginName;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        parseName();
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
    }

    public PluginMenuItemShowData getShowData() {
        return showData;
    }

    public void setShowData(PluginMenuItemShowData showData) {
        this.showData = showData;
    }

    public List<PluginMenuItem> getSubMenuItems() {
        return subMenuItems;
    }

    public void setSubMenuItems(List<PluginMenuItem> subMenuItems) {
        this.subMenuItems = subMenuItems;
    }

    public long getRecentUsedTime() {
        return recentUsedTime;
    }

    public void setRecentUsedTime(long recentUsedTime) {
        this.recentUsedTime = recentUsedTime;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public int getMenuLevel() {
        return menuLevel;
    }

    public void setMenuLevel(int menuLevel) {
        this.menuLevel = menuLevel;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
        parseDesc();
    }

    public boolean isMenuEnable() {
        return menuEnable;
    }

    public void setMenuEnable(boolean menuEnable) {
        this.menuEnable = menuEnable;
    }

    /**
     * Parse name
     */
    private void parseName() {
        if (nameMap != null) {
            nameMap.clear();
            nameMap = null;
        }
        try {
            nameMap = JSON.parseObject(name, new TypeReference<Map<String, String>>() {
            });


        } catch (JSONException e) {
            nameMap = null;

        }


    }

    public String getLangName(String lang) {
        if (nameMap == null || nameMap.isEmpty()) {
            return name;
        }
        String langName = nameMap.get(lang);

        if (TextUtils.isEmpty(langName)) {
            langName = nameMap.get("en");
        }
        if (TextUtils.isEmpty(langName)) {
            langName = nameMap.get("zh_CN");
        }
        if (TextUtils.isEmpty(langName)) {
            langName = nameMap.get("zh_TW");
        }
        if (TextUtils.isEmpty(langName)) {
            langName = nameMap.get("ja");
        }
        if (TextUtils.isEmpty(langName)) {
            langName = name;
        }
        return langName;
    }

    private void parseDesc() {
        if (descMap != null) {
            descMap.clear();
            descMap = null;
        }
        try {
            descMap = JSON.parseObject(desc, new TypeReference<Map<String, String>>() {
            });
        } catch (JSONException e) {
            nameMap = null;
        }
    }

    public String getLangDesc(String lang) {
        if(descMap == null || descMap.isEmpty()) {
            return  desc;
        }
        String langDesc = descMap.get(lang);
        if(TextUtils.isEmpty(langDesc)) {
            langDesc = descMap.get("en");
        }
        if (TextUtils.isEmpty(langDesc)) {
            langDesc = descMap.get("zh_CN");
        }
        if (TextUtils.isEmpty(langDesc)) {
            langDesc = descMap.get("zh_TW");
        }
        if (TextUtils.isEmpty(langDesc)) {
            langDesc = descMap.get("ja");
        }
        if (TextUtils.isEmpty(langDesc)) {
            langDesc = desc;
        }
        return langDesc;
    }

    @Override
    public String toString() {
        return "PluginMenuItem{" +
                "pluginID='" + pluginID + '\'' +
                ", pluginName='" + pluginName + '\'' +
                ", type=" + type +
                ", parentId=" + parentId +
                ", id=" + id +
                ", name='" + name + '\'' +
                ", color=" + color +
                ", icon='" + icon + '\'' +
                ", bgColor=" + bgColor +
                ", enable=" + enable +
                ", showData=" + showData +
                ", subMenuItems=" + subMenuItems +
                ", recentUsedTime=" + recentUsedTime +
                '}';
    }
}
